package id.web.zaniar.oraboros;

class GameEngine {
    //Singleton class instance
    private static var instance : GameEngine;

    //Stack of states
    private var states : List<GameState>;

    public static function getInstance() {
        if(instance == null)
            instance = new GameEngine();

        return instance;
    }

    //Singleton constructor
    private function new() {
        states = new List<GameState>();
    }

    //Change current state with new state
    public function changeState(state:GameState) {
        states.first().deactivate();
        popState();
        pushState(state);
        states.first().activate();
    }

    //Insert new state to the stack and switch to the new state
    public function pushState(state:GameState) {
        if(states.first() != null) {
            states.first().deactivate();
        }
        states.push(state);
        states.first().activate();
    }

    //Remove the current state and switch to the previous state
    public function popState() {
        states.first().deactivate();
        states.pop();
        if(states.first() != null) {
            states.first().activate();
        }
    }
}
